﻿using System.Web.Mvc;
using App.Domain.Contracts;
using App.Web.Authentication;
using Microsoft.Practices.Unity;
using App.Web.Models;

namespace App.Web.Controllers
{
    public class AuthController : Controller {
        private readonly IFormsAuthentication formsAuthentication;
        private readonly IUserServices userServices;


        [InjectionConstructor]
        public AuthController( IFormsAuthentication formsAuthentication,
                              IUserServices userServices) {
            this.formsAuthentication = formsAuthentication;
            this.userServices = userServices;
        }

        //
        // GET: /Auth/

        public ActionResult SignIn(SignInViewModel singinuser)
        {

            var user = this.userServices.GetOrCreateUser(singinuser.User.DisplayName, singinuser.User.UserPwd);
            if (user != null)
            {
                this.formsAuthentication.SetAuthCookie(this.HttpContext,
                                                               UserAuthenticationTicketBuilder.CreateAuthenticationTicket(
                                                                   user));

                return RedirectToAction("Index", "Company");
            }
            return this.RedirectToAction("Index", "Home");
            
        }

        public ActionResult SignInWithProvider(string providerUrl) {
            return RedirectToAction("Index", "Company");
        }

        public ActionResult SignInResponse(string returnUrl) {
            return View();
        }

        public ActionResult SignOut() {
            this.formsAuthentication.Signout();
            return this.RedirectToAction("Index", "Home");
        }
    }
}
